import BaseView from "../../../cfw/mvc/BaseView";
import GlobalEvent from "../../../cfw/event/GlobalEvent";
import { EventName } from "../../../public/Config";
import CookeryController from "../CookeryController";
import { ResType } from "../../../cfw/res/ResInterface";
import ModuleManager, { ModuleID } from "../../../cfw/module/ModuleManager";
import SDKManager from "../../../sdk/SDKManager";
import { SDKEventName } from "../../../sdk/SDKConfig";


const { ccclass, property } = cc._decorator;

@ccclass
export default class CookeryRoomView extends BaseView {


    // @property([CookeryRoomStep])
    // steps: CookeryRoomStep[] = []

    @property(cc.Node)
    content: cc.Node = null;

    private step: number = 0;

    private len: number = 9;

    protected controller: CookeryController;
    protected childComp: cc.Component;
    start() {
        this.controller = CookeryController.instance();
        GlobalEvent.instance().on(EventName.UPDATE_COOKIE_STEP, this.updateStep, this)
        this.startStep(this.step)
    }

    onButtonHomeClick() {
        this.controller.back();
    }
    private tempComp = null;
    startStep(n: number) {
        // this.steps[n].start();
        // this.steps[n].node.opacity = 0;
        // cc.tween(this.steps[n].node).to(1, { opacity: 255 }).start()
        // this.content.removeAllChildren()
        this.tempComp = this.childComp;

        this.controller.addToParent('prefabs/CookeryRoomStep' + n, 'CookeryRoomStep' + n, this.model, ModuleManager.getLoader(), this.content, (comp) => {
            this.childComp = comp
            if (this.tempComp) {
                this.tempComp.node.destroy()
                this.tempComp = null;
            }
        })
    }

    updateStep(step) {
        this.step = step;
        // this.steps[this.step].node.active = false;
        SDKManager.getChannel().sendEvent(SDKEventName.make, this.step)
        this.step++;
        if (this.step >= this.len) {
            this.onButtonHomeClick()
        } else {
            this.startStep(this.step)
        }


    }

    onDestroy() {
        GlobalEvent.instance().off(EventName.UPDATE_COOKIE_STEP, this.updateStep, this)
        ModuleManager.getLoader(ModuleID.COOKIE).release()
    }

}
